//
//  ZPVisitorView.swift
//  weibo
//
//  Created by zhangpeng on 16/4/16.
//  Copyright © 2016年 zhangpeng. All rights reserved.
//

import UIKit

import SnapKit

class ZPVisitorView: UIView {

    
    //定义一个闭包
    var visitorClosure:(()->())?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    //MARK : - 外界调用的接口
    func setVisitorInfo(imgName:String? ,title:String?){
    
        if let img = imgName, tit = title{
        
            iconImageView.image = UIImage(named: img)
            massageLable.text = tit
            
            //隐藏圆圈
            circleImageView.hidden = true
        }else{
        
            //说明首页
            setCircleAnim()
        }
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
            
        }
   
    //让圆圈转起来
    private func setCircleAnim(){
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        //设置动画时间
        anim.duration = 15

          // 设置toValue
        anim.toValue = 2 * M_PI
        // 动画次数repeatCount
        anim.repeatCount = MAXFLOAT
        // 当我们切换tabbar 或者 程序退到后台 默认动画被移除
        
        anim.removedOnCompletion = false
        
        circleImageView.layer.addAnimation(anim, forKey: nil)
    }

    private func setupUI(){
    
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        //添加控件
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(massageLable)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //添加约束
        // 01
        circleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
            
        }
        // 02
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
        // 03
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        // 04
        massageLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(maskImageView)
            make.height.equalTo(36)
            make.width.equalTo(230)
        }
        // 05
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(massageLable)
            make.top.equalTo(massageLable.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        // 06
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(massageLable)
            make.top.equalTo(registerButton)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
    }
    
    
    //MARK :  - 懒加载控件
    // 圆圈
    private lazy var circleImageView :UIImageView = UIImageView (image: UIImage(named:"visitordiscover_feed_image_smallicon"))
      // 遮挡
    private lazy var maskImageView:UIImageView = {
    
        let img = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return img
    }()
    
      // 小房子
    private lazy var iconImageView:UIImageView = UIImageView (image: UIImage(named:"visitordiscover_feed_image_house"))
    
    // 文字
    private lazy var massageLable:UILabel = {
    
        let lab = UILabel(textColor: UIColor.grayColor(), textFont: 14)
        
        
        lab.numberOfLines = 0
        
        //对其方法
        lab.textAlignment = .Center
        
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        return lab
    }()
    
    //创建注册按钮
    private lazy var registerButton:UIButton = {
    
        let button = UIButton()
        
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
    
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        return button
    }()
    
    //创建登录按钮
    private lazy var loginButton:UIButton = {
        
        let button = UIButton()
        
         button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        return button
        }()
    
    //
           @objc private func loginClick(){
    
        visitorClosure?()
    }
    
}

extension ZPVisitorView {

    
}
